import tkinter as tk

class DraggableDataBlock:
    def __init__(self, canvas, y, height, text):
        self.canvas = canvas
        self.text = text
        self.height = height
        self.id = canvas.create_text(10, y + height/2, text=text, anchor="w")
        self.rect_id = canvas.create_rectangle(0, y, 50, y + height, outline="", fill="lightblue")
        
        # 绑定鼠标事件
        self.canvas.tag_bind(self.rect_id, "<Button-1>", self.on_press)
        self.canvas.tag_bind(self.rect_id, "<B1-Motion>", self.on_drag)
        
        self.y = y
        self.drag_y_offset = 0

    def on_press(self, event):
        # 计算鼠标点击位置与矩形顶部的偏移
        self.drag_y_offset = event.y - self.y

    def on_drag(self, event):
        # 计算新的y位置
        if event.y > 0 and event.y < self.canvas.winfo_height():
            new_y = event.y - self.drag_y_offset
            
            # 更新矩形位置
            self.canvas.coords(self.rect_id, 0, new_y, 50, new_y + self.height)
            self.canvas.coords(self.id, 10, new_y + self.height/2)
            
            self.y = new_y


class Dashboard:
    def __init__(self, root):
        self.root = root
        self.root.title("数据看板")
        
        # 创建主画布
        self.canvas = tk.Canvas(root, width=200, height=300, bg="white")
        self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        
        # 创建可拖动的数据块
        self.data_blocks = [
            DraggableDataBlock(self.canvas, 50, 40, "数据a"),
            DraggableDataBlock(self.canvas, 100, 30, "数据b"),
            DraggableDataBlock(self.canvas, 140, 35, "数据c")
        ]
        
        # 创建内容区域
        self.content_frame = tk.Frame(root)
        self.content_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        
        # 创建水平刻度
        self.create_scale()
    
    def create_scale(self):
        # 创建水平刻度
        scale_frame = tk.Frame(self.content_frame, height=20)
        scale_frame.pack(fill=tk.X)
        
        tk.Label(scale_frame, text="0").pack(side=tk.LEFT)
        tk.Label(scale_frame, text="75").pack(side=tk.LEFT, expand=True)
        tk.Label(scale_frame, text="150").pack(side=tk.LEFT)
        
        # 创建刻度线
        scale_line = tk.Canvas(self.content_frame, height=10, bg="white")
        scale_line.pack(fill=tk.X)
        
        # 绘制刻度线
        scale_line.create_line(0, 5, 200, 5, fill="black")
        scale_line.create_line(0, 5, 0, 0, fill="black")
        scale_line.create_line(150, 5, 150, 0, fill="black")
        scale_line.create_line(200, 5, 200, 0, fill="black")


if __name__ == "__main__":
    root = tk.Tk()
    app = Dashboard(root)
    root.mainloop()